<template>
    <div
        class="page"
        :style="gameNav==1?'padding-bottom: 116px;':''"
    >
        <van-nav-bar
            title="战况详情"
            left-arrow
            fixed
            @click-left="goBack"
        />

        <div class="game-result-box">
            <div
                class="game-result"
                :class="gameDetail.win==1?'red-winner':''||gameDetail.win==2?'red-loser':''||gameDetail.win==3?'red-tie':''"
            >
                <div
                    class="game-user"
                    :class="userInfo.user_id == item.user_list.user_id?'owner':''"
                    v-for="(item,index) in redPlayer"
                    :key="index"
                >
                    <div class="user-header">
                        <van-image
                            round
                            fit="cover"
                            :src="item.user_list.heard_img"
                        />
                    </div>
                    <div class="user-name van-ellipsis">{{item.user_list.nickname}}</div>
                    <div class="user-level">等级：{{item.rank.rank_name}}</div>
                </div>
            </div>
            <div
                class="game-result"
                :class="gameDetail.win==1?'blue-loser':''||gameDetail.win==2?'blue-winner':''||gameDetail.win==3?'blue-tie':''"
            >
                <div
                    class="game-user"
                    :class="userInfo.user_id == item.user_list.user_id?'owner':''"
                    v-for="(item,index) in bluePlayer"
                    :key="index"
                >
                    <div class="user-header">
                        <van-image
                            round
                            fit="cover"
                            :src="item.user_list.heard_img"
                        />
                    </div>
                    <div class="user-name van-ellipsis">{{item.user_list.nickname}}</div>
                    <div class="user-level">等级：{{item.rank.rank_name}}</div>
                </div>
            </div>
        </div>

        <!-- <div class="user-game-info">
            <div class="user-game-info-item red">
                <div>玩家胜率：66.7%</div>
            </div>
            <div class="user-game-info-item gray">
                <div>总场次：2058</div>
            </div>
            <div class="user-game-info-item red">
                <div>总排名：26</div>
            </div>
            <div class="user-game-info-item gray">
                <div>总积分：10000</div>
            </div>
        </div> -->

        <div class="game-detail-box">
            <div class="game-detail-item">
                <div class="game-detail-item-1">玩家</div>
                <div class="game-detail-item-2">职业</div>
                <div class="game-detail-item-3">击杀</div>
                <div class="game-detail-item-4">死亡</div>
                <div class="game-detail-item-5">命中</div>
                <div class="game-detail-item-6">里程m</div>
                <div class="game-detail-item-7">能耗cal</div>
            </div>
            <div
                class="game-detail-item red"
                v-for="item in redPlayer"
                :key="item.game_user_id"
            >
                <div class="game-detail-item-1 van-ellipsis">{{item.user_list.nickname}}</div>
                <div class="game-detail-item-2">{{item.eJob_name}}</div>
                <div class="game-detail-item-3">{{item.kill_num}}</div>
                <div class="game-detail-item-4">{{item.die_num}}</div>
                <div class="game-detail-item-5">{{item.hit_rate}}</div>
                <div class="game-detail-item-6">{{item.move_distance}}</div>
                <div class="game-detail-item-7">{{parseInt(item.cal)}}</div>
            </div>
            <div
                class="game-detail-item blue"
                v-for="item in bluePlayer"
                :key="item.game_user_id"
            >
                <div class="game-detail-item-1 van-ellipsis">{{item.user_list.nickname}}</div>
                <div class="game-detail-item-2">{{item.eJob_name}}</div>
                <div class="game-detail-item-3">{{item.kill_num}}</div>
                <div class="game-detail-item-4">{{item.die_num}}</div>
                <div class="game-detail-item-5">{{item.hit_rate}}</div>
                <div class="game-detail-item-6">{{item.move_distance}}</div>
                <div class="game-detail-item-7">{{parseInt(item.cal)}}</div>
            </div>

            <video-player
                ref="videoPlayer"
                class="video-player vjs-custom-skin"
                :playsinline="playsinline"
                :options="playerOptions"
                v-if="gameDetail.video"
                @canplay="onPlayerCanplay($event)"
            />
        </div>

        <div
            class="game-detail-share"
            @click="showShare"
        >点击分享好友赚积分</div>

        <div class="game-detail-nav">
            <div
                class="game-detail-nav-item"
                :class="gameNav==0?'active':''"
                @click="gameNavClick(0)"
            >更多视频</div>
            <div
                class="game-detail-nav-item"
                :class="gameNav==1?'active':''"
                @click="gameNavClick(1)"
            >用户评论</div>
        </div>

        <div
            class="more-video-box"
            v-show="gameNav==0"
        >
            <img
                src="../assets/more-video-default.jpg"
                @click="goMoreVideo"
            />
        </div>

        <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
            v-show="gameNav==1"
        >
            <div class="video-comment-box">
                <div
                    class="video-comment-item"
                    v-for="(item,index) in gameComment"
                    :key="index"
                >
                    <van-image
                        class="comment-user-header"
                        round
                        fit="cover"
                        :src="item.user.heard_img"
                    />
                    <div class="comment-info">
                        <div class="comment-user-name van-ellipsis">{{item.user.nickname}}</div>
                        <div class="comment-date">{{item.add_time}}</div>
                        <div class="comment-detail">{{item.content}}</div>
                    </div>
                </div>
            </div>
        </van-list>

        <div
            class="comment-input-box"
            v-show="gameNav==1"
        >
            <input
                class="comment-input"
                type="text"
                placeholder="期待你的精彩评论"
                v-model="commentDetail"
            />
            <div
                class="comment-btn"
                @click="goComment"
            >评论</div>
        </div>

        <van-overlay
            :show="shareShow"
            @click="shareShow = false"
        >
            <div
                class="share-main"
                ref="shareMain"
                @click.stop
            >
                <slot>
                    <div class="share-user-info">
                        <div class="share-user-header">
                            <van-image
                                round
                                fit="cover"
                                :src="userInfo.heard_img"
                            />
                        </div>
                        <div class="share-user-name van-ellipsis">{{userInfo.nickname}}</div>
                        <div
                            class="share-user-level"
                            v-if="userInfo.integral_rank"
                        >等级：{{userInfo.integral_rank.rank_name}}</div>
                        <div
                            class="share-user-result result-1"
                            v-if="userInfo.game"
                        >玩家胜率：{{userInfo.game.can_win}}</div>
                        <div
                            class="share-user-result result-2"
                            v-if="userInfo.game"
                        >总场次：{{userInfo.game.all}}</div>
                        <div
                            class="share-user-result result-1"
                            v-if="userInfo.score_order"
                        >总排名：{{userInfo.score_order.rownum}}</div>
                        <div
                            class="share-user-result result-2"
                            v-if="userInfo.integral_order"
                        >总积分：{{userInfo.integral_order.integral}}</div>
                        <img
                            class="share-ewm-img"
                            :src="userCode"
                        />
                    </div>
                </slot>
            </div>
            <div
                class="share-img"
                @click.stop
            ></div>
            <div class="share-tips">长按图片发送给好友</div>
        </van-overlay>
    </div>
</template>

<script>
import $ from 'jquery'
export default {
    data () {
        return {
            gameId: null,
            userInfo: {},
            userCode: '',
            gameDetail: {},
            redPlayer: [],
            bluePlayer: [],
            gameNav: 0,
            gameComment: [],
            nowPage: 0,
            playerOptions: {
                aspectRatio: '16:9',
                fluid: true,
                preload: 'auto',
                sources: [
                    {
                        type: 'video/mp4',
                        src: ''
                    }
                ]
            },
            loading: false,
            finished: false,
            shareShow: false,
            commentDetail: ''
        }
    },
    created () {
        this.gameId = this.$route.query.game_id
        this.getUserInfo()
        this.getGameDetail()
    },
    beforeRouteEnter (to, from, next) {
        // 添加背景色
        window.document.body.style.backgroundColor = '#f6f6f6'
        next()
    },
    beforeRouteLeave (to, from, next) {
        // 去除背景色
        window.document.body.style.backgroundColor = ''
        next()
    },
    computed: {
        playsinline () {
            var ua = navigator.userAgent.toLocaleLowerCase();
            //x5内核
            if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
                return false
            } else {
                //ios端
                return true
            }
        }
    },
    methods: {
        getUserInfo () {
            this.$request({
                url: '/api/user/user_info',
                method: 'post',
                params: {}
            }).then(res => {
                this.userInfo = res
                this.getUserCode()
            })
        },
        getUserCode () {
            this.$request({
                url: '/api/Gzhqrcode/qrcode',
                method: 'post',
                params: {
                    id: this.userInfo.user_id,
                    game_id: this.gameId
                }
            }).then(res => {
                this.userCode = res
            })
        },
        getGameDetail () {
            this.$request({
                url: '/api/Game/game_list',
                method: 'post',
                params: {
                    game_id: this.gameId,
                    paramter: 1
                }
            }).then(res => {
                res[0].game_user.forEach((item) => {
                    if (item.camp == 1) {
                        this.redPlayer.push(item)
                    }
                    else {
                        this.bluePlayer.push(item)
                    }
                })
                this.gameDetail = res[0]
                this.playerOptions.sources[0].src = res[0].video
            })
        },
        getGameComment () {
            this.$request({
                url: '/api/Moments/moment_list',
                method: 'post',
                params: {
                    game_id: this.gameId,
                    order_by: 'add_time',
                    page: this.nowPage,
                    limit: 10
                }
            }).then(res => {
                this.loading = false
                this.nowPage = this.nowPage + 1
                this.gameComment = this.gameComment.concat(res)
            }).catch(res => {
                this.finished = true
            })
        },
        onLoad () {
            if (this.gameNav == 1) {
                this.getGameComment()
            }
        },
        goBack () {
            if (window.history.length <= 1) {
                this.$router.push('/user')
            }
            else {
                this.$router.go(-1)
            }
        },
        showShare () {
            const that = this
            this.$toast.loading({
                message: '图片生成中...',
                forbidClick: true,
                duration: 0
            })
            this.shareShow = true
            window.scrollTo(0, 0)
            setTimeout(() => {
                that.$html2canvas(that.$refs.shareMain, {
                    useCORS: true, //（图片跨域相关）
                    allowTaint: false, //允许跨域（图片跨域相关）
                    taintTest: true, //是否在渲染前测试图片
                }).then(canvas => {
                    let dataURL = canvas.toDataURL('image/png')
                    document.querySelector('.share-img').innerHTML =
                        '<img width="100%" src="' + dataURL + '" />'
                    setTimeout(() => {
                        that.$toast.clear()
                    }, 1000)
                })
            }, 2000)
        },
        gameNavClick (index) {
            if (this.gameNav == index) {
                return
            }
            this.gameNav = index
        },
        goMoreVideo () {
            this.$router.push('/video')
        },
        goComment () {
            if (this.commentDetail == '') {
                this.$toast('请输入评论内容')
                return
            }
            this.$request({
                url: '/api/Moments/moment_add',
                method: 'post',
                params: {
                    game_id: this.gameId,
                    content: this.commentDetail
                }
            }).then(res => {
                this.$toast.success('评论成功')
                this.commentDetail = ''
                this.gameComment = []
                this.nowPage = 0
                this.finished = false
                this.getGameComment()
            }).catch(res => {
                this.$toast.fail('评论失败，请稍后再试')
            })
        },
        onPlayerCanplay (player) {
            // console.log('player Canplay!', player)
            //解决自动全屏
            var ua = navigator.userAgent.toLocaleLowerCase();
            //x5内核
            if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {

                $('body').find('video').attr('x-webkit-airplay', true).attr('x5-playsinline', true).attr('webkit-playsinline', true).attr('playsinline', true)
            } else {
                //ios端
                $('body').find('video').attr('webkit-playsinline', "true").attr('playsinline', "true")

            }
        }
    }
}
</script>

<style lang="less" scoped>
.page {
    padding-top: 92px;
}

.game-result-box {
    display: flex;
    justify-content: space-between;
    padding: 30px;
}

.game-result {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 340px;
    height: 340px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

.red-winner {
    background-image: url("../assets/game-red-winner.png");
}
.red-loser {
    background-image: url("../assets/game-red-loser.png");
}

.red-tie {
    background-image: url("../assets/game-red-tie.png");
}

.blue-winner {
    background-image: url("../assets/game-blue-winner.png");
}
.blue-loser {
    background-image: url("../assets/game-blue-loser.png");
}

.blue-tie {
    background-image: url("../assets/game-blue-tie.png");
}

.game-user {
    text-align: center;
    margin: 8px 0;
}

.user-header {
    width: 76px;
    height: 76px;
    background: #fff;
    padding: 6px;
    box-sizing: border-box;
    border-radius: 50%;
    margin: 0 auto;
    .van-image {
        width: 100%;
        height: 100%;
    }
}

.user-name {
    font-weight: 600;
    font-size: 22px;
    color: #ffffff;
    margin-top: 6px;
}

.user-level {
    font-weight: 500;
    font-size: 18px;
    color: #fff;
    margin-top: 4px;
}

.owner {
    .user-name,
    .user-level {
        color: #fff000;
    }
}

.user-game-info {
    display: flex;
    justify-content: space-between;
    padding: 20px 30px 30px 30px;
    .red {
        background-color: #df1a24;
    }
    .gray {
        background-color: #434343;
    }
}

.user-game-info-item {
    width: 164px;
    height: 30px;
    div {
        width: 328px;
        height: 60px;
        line-height: 60px;
        font-size: 36px;
        color: #fff;
        text-align: center;
        -webkit-transform-origin-x: 0;
        -webkit-transform-origin-y: 0;
        transform: scale(0.5);
    }
}

.game-detail-box {
    padding: 0 30px 30px 30px;
    background-color: #fff;
    .red {
        background-color: #ee8d91;
        border-radius: 17px;
        border: solid 2px #e62129;
        color: #fff;
        margin-bottom: 12px;
    }
    .blue {
        background-color: #9698c4;
        border-radius: 17px;
        border: solid 2px #2e3089;
        color: #fff;
        margin-bottom: 12px;
    }
}

.game-detail-item {
    display: flex;
    align-items: center;
    height: 68px;
    color: #434343;
    box-sizing: border-box;
}

.game-detail-item-1 {
    width: 190px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-2 {
    width: 74px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-3 {
    width: 64px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-4 {
    width: 64px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-5 {
    width: 70px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-6 {
    width: 96px;
    text-align: center;
    font-size: 20px;
}

.game-detail-item-7 {
    flex: 1;
    text-align: center;
    font-size: 20px;
}

.game-detail-share {
    height: 120px;
    line-height: 120px;
    text-align: center;
    background-color: #e01d25;
    font-weight: 600;
    font-size: 32px;
    color: #fff;
    text-shadow: 1px 1px 1px #000;
    margin: 20px 0;
}

.game-detail-nav {
    display: flex;
    margin-top: 22px;
    background-color: #fff;
    .active {
        border-color: #e62129;
        color: #df1a24;
    }
}

.game-detail-nav-item {
    flex: 1;
    height: 90px;
    line-height: 90px;
    text-align: center;
    border-bottom: 4px solid #fff;
    font-weight: 500;
    font-size: 32px;
    color: #434343;
}

.more-video-box {
    img {
        width: 100%;
        height: auto;
    }
}

.video-comment-box {
    position: relative;
    padding: 30px 30px 0 30px;
}

.video-comment-item {
    display: flex;
    margin-bottom: 30px;
}

.comment-user-header {
    width: 78px;
    height: 78px;
}

.comment-info {
    flex: 1;
    padding-left: 20px;
}

.comment-user-name {
    margin-top: 10px;
    font-weight: 500;
    font-size: 28px;
    color: #0c0c0c;
}

.comment-date {
    font-weight: 400;
    font-size: 20px;
    color: #999;
    margin-top: 10px;
}

.comment-detail {
    font-weight: 400;
    font-size: 28px;
    color: #0c0c0c;
    margin-top: 10px;
}

.comment-input-box {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 116px;
    background-color: #fff;
    border-top: 1px solid #eee;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    padding: 0 30px;
}

.comment-input {
    flex: 1;
    height: 68px;
    padding: 0 30px;
    font-weight: 400;
    font-size: 26px;
    color: #222;
    border: 1px solid #e5e5e5;
    border-radius: 6px;
    box-sizing: border-box;
}

.comment-btn {
    width: 120px;
    height: 68px;
    line-height: 68px;
    text-align: center;
    background-color: #e62129;
    font-size: 26px;
    color: #fff;
    border-radius: 6px;
    margin-left: 20px;
}

.share-main {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.5);
    width: 1080px;
    height: 1920px;
    background-image: url("../assets/share-img.jpg");
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

.share-tips {
    position: absolute;
    bottom: 5%;
    left: 0;
    right: 0;
    text-align: center;
    font-weight: 500;
    font-size: 30px;
    color: #fff;
}

.share-user-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 120px;
}

.share-user-header {
    width: 186px;
    height: 186px;
    background: #fff;
    padding: 12px;
    box-sizing: border-box;
    border-radius: 50%;
    .van-image {
        width: 100%;
        height: 100%;
    }
}

.share-user-name {
    font-weight: bold;
    font-size: 60px;
    color: #fff;
    margin-top: 20px;
}

.share-user-level {
    font-weight: 500;
    font-size: 50px;
    color: #fff;
    margin-top: 10px;
    margin-bottom: 50px;
}

.share-user-result {
    width: 378px;
    height: 70px;
    line-height: 70px;
    text-align: center;
    font-weight: 500;
    font-size: 28px;
    color: #fff;
    margin-bottom: 24px;
}

.result-1 {
    background-color: #aa0103;
}

.result-2 {
    background-color: #434343;
}

.share-ewm-img {
    position: absolute;
    bottom: 67px;
    right: 74px;
    width: 160px;
    height: 160px;
}

.share-img {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.5);
    width: 1080px;
    height: 1920px;
    img {
        width: 100%;
        height: 100;
    }
}
</style>